// pages/login/login.js
const app = getApp()
Page({
  data: {
    showAuthorize:true
  },

  onLoad: function (options) {
    this.login();
  },
  login: function () {
    let that=this;
    wx.login({
      success: res => {
        if (res.code) {  //有code
          app.globalData.code = res.code;
          wx.getSetting({
            success: function (res) {  //已授权
            console.log(res)
              if (res.authSetting['scope.userInfo']) {
                wx.getUserInfo({  //调用用户信息
                  success: res => { //获取信息            
                    console.log(res)
                    wx.setStorageSync('headurl', res.userInfo.avatarUrl);
                    wx.setStorageSync('username', res.userInfo.nickName);
                    wx.request({   //传后台
                      url: app.globalData.url + '/jxb/login/login/loginAuth',
                      method: 'POST',
                      header: {
                        'content-type': 'application/x-www-form-urlencoded'
                      },
                      data: {
                        code: app.globalData.code,
                        encryptedData: res.encryptedData,
                        iv: res.iv
                      },
                      success: function (res3) {               
                        wx.setStorageSync('token', res3.data.data.token);
                        wx.setStorageSync('uid', res3.data.data.uid);                                                        
                      },

                    })
                  }
                })
              } else {  //未授权
                that.getUserInfo();
              }
            }

          });// 查看授权


        } else {     //没有code
         
        }
      }
    })
  },
  getUserInfo(res) {
    console.log(res);
    if (res.detail.userInfo) {
      console.log("点击了同意授权");
    } else {
      console.log("点击了拒绝授权");
    }
  },
  getPhoneNumber: function (e) {
    var that = this;
    console.log(e)
    console.log(e.detail.errMsg == "getPhoneNumber:ok");
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      // wx.request({
      //   url: 'http://localhost/index/users/decodePhone',
      //   data: {
      //     encryptedData: e.detail.encryptedData,
      //     iv: e.detail.iv,
      //     sessionKey: that.data.session_key,
      //     uid: "",
      //   },
      //   method: "post",
      //   success: function (res) {
      //     console.log(res);
      //   }
      // })
    }
  },
 
})